package logic

import (
	"context"
	"fmt"
	"qianhui/models/mysqlinit"
	"qianhui/pkg"

	"qianhui/qianhuirpc/internal/svc"
	"qianhui/qianhuirpc/qianhuirpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *qianhuirpc.RegisterRequest) (*qianhuirpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	var register mysqlinit.User
	mysqlinit.DB.Debug().Where("account=?", in.Account).Find(&register)
	if register.ID != 0 {
		if register.ID != 0 {
			return nil, fmt.Errorf("账号注册重复，请重新输入")
		}
	}
	user := mysqlinit.User{

		Account:  in.Account,
		Password: pkg.Md5Password(in.Password),
	}
	mysqlinit.DB.Debug().Create(&user)
	return &qianhuirpc.RegisterResponse{
		Id: int64(user.ID),
	}, nil
}
